package LambdaRepresentation;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectClassDemo {
    //通过反射创建对象
    public static void reflectNewInstance(){
        Class<?> c1 = null;
        try{
            c1 = Class.forName("LambdaRepresentation.Student");   //因为这个类他在src底下的包里,所以要加上包名,否则会报错
            Student student = (Student)c1.newInstance();

            System.out.println(student);
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


    //通过构造方法创建对象
    public static void reflectPrivateConstructor(){
        Class<Student> c1 = null;
        try{
            c1 = (Class<Student>) Class.forName("LambdaRepresentation.Student");   //因为这个类他在src底下的包里,所以要加上包名,否则会报错
            Constructor<Student> constructor = c1.getDeclaredConstructor(String.class,int.class);

            //因为私有的不能反射为非私有的,所以这里要加一个setAccessible将其转换为1非私有的
            constructor.setAccessible(true);

            //获取构造方法
            Student student = constructor.newInstance("佩奇",10);

            System.out.println(student);

        }catch (ClassNotFoundException e){
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


    //反射私有属性
    public static void reflectPrivateField(){
        Class<Student> c1 = null;
        try{
            c1 = (Class<Student>) Class.forName("LambdaRepresentation.Student");   //因为这个类他在src底下的包里,所以要加上包名,否则会报错
            Field field = c1.getDeclaredField("name");
            field.setAccessible(true);

            //通过instance拿到学生对象
            Student student = (Student)c1.newInstance();
            field.set(student,"喜羊羊");

            System.out.println(student);

        }catch (ClassNotFoundException e){
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    //反射私有方法
    public static void reflectPrivateMethod(){
        Class<Student> c1 = null;
        try{
            c1 = (Class<Student>) Class.forName("LambdaRepresentation.Student");   //因为这个类他在src底下的包里,所以要加上包名,否则会报错
            Method method = c1.getDeclaredMethod("function",String.class);


            method.setAccessible(true);

            //通过instance拿到学生对象
            Student student = (Student)c1.newInstance();
            method.invoke(student,"Hello Aileen");

;

        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }  catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    //测试反射
    public static void main(String[] args) {

        reflectPrivateMethod();

        //reflectPrivateField();

        //reflectPrivateConstructor();

        //reflectNewInstance();
    }

}
